package com.example.shop.modules.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.shop.modules.manage.service.IGoodsDetailAdminService;
import com.example.shop.modules.manage.service.IGoodsAdminService;
import com.example.shop.persistence.entity.Goods;
import com.example.shop.persistence.entity.GoodsDetail;
import com.example.shop.utils.JSONResult;
import com.example.shop.utils.MPPageFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "后台接口商品表")
@RestController
@RequestMapping("/manage/goods")
public class GoodsAdminController {

    @Autowired
    private IGoodsAdminService goodsService;

    @Autowired
    private IGoodsDetailAdminService goodsDetailService;

    @ApiOperation(value = "查询商品详情",notes = "通过ID查商品详情")
    @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "Int", paramType = "path")
    @GetMapping("/{id}")
    public JSONResult getGoods(@PathVariable("id") Integer id){
        return JSONResult.renderSuccess(goodsService.selectGoodsInfoAll(id));
    }

    /**
     * 查询商品列表
     * @param page
     * @param pageSize
     * @return List
     */
    @ApiOperation(value = "查询商品列表",notes = "分页查询加条件")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name="state", value = "状态：0 不显示  1显示", dataType = "Integer"),
            @ApiImplicitParam(name="sortIndex", value = "排序：0默认排序  1销量   2价格从低到高 3价格从高到低 4库存量从低到高", dataType = "Integer")
    })
    public JSONResult selectGoodsList(Integer page, Integer pageSize, Integer typeId, String name, Integer state, Integer sortIndex){
        // 分页数据
        Page<Goods> pages = MPPageFactory.createPage(page, pageSize);
        // 条件语句
        QueryWrapper<Goods> ew = new QueryWrapper<>();
        if(typeId != null){
            ew.eq("typeId", typeId);
        }
        if(!StringUtils.isEmpty(name)){
            // 模糊查询 商品名称
            ew.like("name", name);
        }
        if(state != null){
            ew.eq("state", state);
        }
        if(sortIndex != null){
            switch(sortIndex){
                case 1: // 按销量排序
                    ew.orderByDesc("sellCount");
                    break;
                case 2: // 价格从低到高排序
                    ew.orderByAsc("price");
                    break;
                case 3: // 价格从高到低排序
                    ew.orderByDesc("price");
                    break;
                case 4: // 库存从低到高
                    ew.orderByAsc("stock");
                    break;
            }
        }else{
            // 按照创建时间排序
            ew.orderByDesc("createTime");
        }
        //查询结果
        IPage<Goods> list = goodsService.page(pages,ew);
        return JSONResult.renderSuccess(list);
    }

    /**
     * 新增商品
     * @param goods, goodsDetail
     * @return List
     */
    @ApiOperation(value = "新增或更新商品",notes = "新增商品详细信息，关联表新增")
    @PostMapping("/insert")
    public JSONResult insertGoodsAll(Goods goods, GoodsDetail goodsDetail){

        goodsService.saveOrUpdate(goods);
        // 获取 新增的goods数据ID，赋值给 goodsDetail表的goodsId字段
        goodsDetail.setGoodsId(goods.getId());
        // 清空详情枚举的ID（这里的ID是 goods 表的ID）
        goodsDetail.setId(null);
        QueryWrapper ew = new QueryWrapper();
        ew.eq("goodsId",goods.getId());
        // 商品详情表更新
        goodsDetailService.saveOrUpdate(goodsDetail,ew);
        return JSONResult.renderSuccess();
    }

    /**
     * 删除商品
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除商品",notes = "可批量删除")
    @DeleteMapping
    public JSONResult deleteGoods(String ids){
        // 非空字符串判断
        if(StringUtils.isEmpty(ids)){
            return JSONResult.renderError();
        }
        // 字符串转数组
        String[] strArray =  ids.split(",");
        goodsService.deleteGoods(strArray);
        return JSONResult.renderSuccess();
    }


}
